<div class="layuimini-main">
    <div class="layui-form layuimini-form" lay-filter="editForm">

        <div class="layui-form-item">
            <input type="hidden" name="id" id="id">
            <label class="layui-form-label required">名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="名称不能为空" placeholder="请输入名称" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">开始时间</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="startTime"name="startTime" placeholder="开始时间">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">结束时间</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="endTime" name="endTime" placeholder="结束时间">
            </div>
        </div>

        <div class="layui-form-item">
          <label class="layui-form-label required" >年级</label>
          <div class="layui-input-block">
              <select name="gradeId" id="gradeId" lay-verify="required"></select>
          </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">班级</label>
            <div class="layui-input-block">
                <input type="text" name="className" id="className" autocomplete="off"  placeholder="班级不能为空"  readonly  class="layui-input"/>
                <input type="hidden" name="classId" id="classId"/>
            </div>
        </div>


        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注信息</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>



        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="editBtn">确认编辑</button>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'table','axios','laydate','selectOrg'], function () {
        let form = layui.form,
            layer = layui.layer,
            axios = layui.axios,
            laydate = layui.laydate,
            selectOrg = layui.selectOrg,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        

        selectOrg.render({
            elem:'#className',
            selected:[],
            checked:false,
            url:'org/tree',
            done:function (data) {
                $('#className').val(data[0].name)
                $('#classId').val(data[0].id)
            }
        })

        // 当前弹出层，防止ID被覆盖
        let parentIndex = layer.index;

        var endDate = laydate.render({
            elem: '#endTime',
            eventElem: '.date-input-icon',
            done: function(value, date) {
                console.log(value)
                console.log(date)


                startDate.config.max = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                }
            }
        });
        var startDate = laydate.render({
            elem: '#startTime',
            eventElem: '.date-input-icon',
            done: function(value, date) {
                console.log(value)
                console.log(date)
                endDate.config.min = {
                    year: date.year,
                    month: date.month - 1,
                    date: date.date,
                };
            }
        });

        //监听提交
        form.on('submit(editBtn)', function (data) {
            var DISABLED = 'layui-btn-disabled';
            $(':button').addClass(DISABLED);
            $(':button').attr('disabled', 'disabled');
            data = data.field
            axios.post('selection/edit',data).then(function(response){
              if(response.code==0){
                layer.msg(response.message,{
                    icon:6,
                    time:2000,
                },function(){
                  window.reload();
                  layer.close(parentIndex);
                  $(':button').removeClass(DISABLED);
                  $(':button').removeAttr('disabled');
                });
              }else{
                layer.msg(response.message,{
                  icon:5,
                  time:2000,
                  anim:6
                },function(){
                  $(':button').removeClass(DISABLED);
                  $(':button').removeAttr('disabled');
                });
              }  
            }).catch(function (error) {
                layer.msg("请求错误",{
                  icon:5,
                  time:2000,
                  anim:6
                },function(){
                  $(':button').removeClass(DISABLED);
                  $(':button').removeAttr('disabled');
                });
            });
            return false;
        });

    });
</script>